#include "inputfolderdialog.h"
#include "ui_inputfolderdialog.h"

inputFolderDialog::inputFolderDialog(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::inputFolderDialog)
{
    m_ui->setupUi(this);

}

inputFolderDialog::~inputFolderDialog()
{
    delete m_ui;
}

void inputFolderDialog::readSettings()
{
    ftpInfoList.clear();

    ftpInfoList.append( new ftpInfo() );
    m_ui->comboBox->addItem( 0 );

    QSettings settings("Folder index", "Settings");
    settings.beginGroup("history");
    QStringList pathList = settings.value( "ftpSourcePaths" ).toString().split( ";", QString::SkipEmptyParts );
    for ( int i = 0; i < pathList.size(); i++ )
    {
        QStringList ftpDataList = pathList.at(i).split( "~", QString::KeepEmptyParts );
        if ( ftpDataList.size() > 3 )
        {
            ftpInfo *fInfo = new ftpInfo();

            fInfo->hostname  = ftpDataList.at(0);
            fInfo->username  = ftpDataList.at(1);
            fInfo->password  = ftpDataList.at(2);
            fInfo->remoteDir = ftpDataList.at(3);

            ftpInfoList.append(fInfo);
            m_ui->comboBox->addItem( fInfo->hostname );
        }
    }

    settings.endGroup();
}

void inputFolderDialog::writeSettings()
{
    QSettings settings("Folder index", "Settings");
    settings.beginGroup("history");
    QString historyLine;

    for ( int i = 0; i < ftpInfoList.size(); i++ )
    {
        if ( !ftpInfoList.at(i)->hostname.isEmpty() )
        {
            QString resLine = ftpInfoList.at(i)->hostname + "~";
            resLine += ftpInfoList.at(i)->username + "~";
            if ( m_ui->rememberPass->isChecked() )
                resLine += ftpInfoList.at(i)->password;
            resLine +=  "~" + ftpInfoList.at(i)->remoteDir;
            resLine += ";";

            historyLine.append( resLine );
        }
    }

    settings.setValue("ftpSourcePaths", historyLine );
    settings.endGroup();
}

void inputFolderDialog::on_comboBox_currentIndexChanged(int index)
{
    if ( index >= 0)
    {
        m_ui->loginEdit->setText( ftpInfoList.at(index)->username );
        m_ui->passEdit->setText( ftpInfoList.at(index)->password );
        m_ui->remoteDirEdit->setText( ftpInfoList.at(index)->remoteDir );
    }
}

void inputFolderDialog::run_form()
{
    readSettings();
    operation = 1;

    exec();
}

ftpInfo* inputFolderDialog::getFtpInfo()
{
    readSettings();

    ftpInfo *fInfo = 0;
    operation = 2;
    if ( exec() )
    {
        fInfo = new ftpInfo();
        fInfo->hostname = m_ui->comboBox->currentText();
        fInfo->username = m_ui->loginEdit->text();
        fInfo->password = m_ui->passEdit->text();
        fInfo->remoteDir = m_ui->remoteDirEdit->text();
    }

    return fInfo;
}

bool inputFolderDialog::checkInputPath()
{
    if ( m_ui->comboBox->currentText().isEmpty() )
    {
        QMessageBox::critical( this, "Error", "Enter ftp path");
        m_ui->comboBox->setFocus();
        return false;
    }
    if ( m_ui->comboBox->currentText().right(1) == "\\" || m_ui->comboBox->currentText().right(1) == "/" )
        m_ui->comboBox->setEditText( m_ui->comboBox->currentText().left( m_ui->comboBox->currentText().length()-1 ) );

    return true;
}

void inputFolderDialog::addPath( const QString& source )
{
    int pathIndex = m_ui->comboBox->findText( source, Qt::MatchExactly );
    if ( pathIndex == -1 )
    {
        ftpInfoList.at( 0 )->hostname = m_ui->comboBox->currentText();
        ftpInfoList.at( 0 )->username = m_ui->loginEdit->text();
        ftpInfoList.at( 0 )->password = m_ui->passEdit->text();
        ftpInfoList.at( 0 )->remoteDir = m_ui->remoteDirEdit->text();
    }
    else
    {
        ftpInfoList.at( pathIndex )->username = m_ui->comboBox->currentText();
        ftpInfoList.at( pathIndex )->username = m_ui->loginEdit->text();
        ftpInfoList.at( pathIndex )->password = m_ui->passEdit->text();
        ftpInfoList.at( pathIndex )->remoteDir = m_ui->remoteDirEdit->text();
    }
}

void inputFolderDialog::on_closeButton_clicked()
{
    reject();
}

void inputFolderDialog::on_okButton_clicked()
{
    if ( checkInputPath() )
    {
        QUrl url( m_ui->comboBox->currentText() );
        if ( !url.isValid() || url.scheme().toLower() != QLatin1String("ftp") )
        {
            QMessageBox::critical( this, "Error", "Invalid ftp path. Try again" );
            m_ui->comboBox->setFocus();
            return;
        }
        addPath( m_ui->comboBox->currentText() );
        writeSettings();

        if ( operation == 1 )
        {
            // start making listing
            MainWindow * listWind = new MainWindow();
            listWind->show();
            QString host = m_ui->comboBox->currentText();
            listWind->run_ftp( m_ui->comboBox->currentText().simplified(),
                               m_ui->loginEdit->text(), m_ui->passEdit->text(),
                               m_ui->remoteDirEdit->text() );
        }
    }
    else
        return;

    accept();
}

void inputFolderDialog::on_comboBox_editTextChanged( QString text )
{
    if ( text.isEmpty() )
        return;

    if ( text.left(1).toLower() != "f" || text.left(2).toLower() != "ft"
         || text.left(3).toLower() != "ftp" || text.left(4).toLower() != "ftp:"
         || text.left(5).toLower() != "ftp:/" )
    {
        changeFtpHost();
    }
}

void inputFolderDialog::changeFtpHost()
{
    disconnect( m_ui->comboBox, SIGNAL( editTextChanged (const QString&)),
                this, SLOT ( on_comboBox_editTextChanged( const QString& ) ) );
    m_ui->comboBox->setEditText( "ftp://" + m_ui->comboBox->currentText() );
    connect( m_ui->comboBox, SIGNAL( editTextChanged (const QString&)),
                this, SLOT ( on_comboBox_editTextChanged( const QString& ) ) );
}
